﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Context;
using Entities;
using WCF_ListenMusic.DataContract;
namespace WCF_ListenMusic
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "AlbumService" in code, svc and config file together.
    public class AlbumService : IAlbumService
    {
        Context.Connect_DBDataContext context = new Context.Connect_DBDataContext();

        #region IAlbumService Members
        public List<Album> getAllAlbum()
        {
            try
            {
                var q = from c in context.Albums
                        select c;
                return q.ToList();
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }
        #endregion

        #region IAlbumService Members


        public List<Album> getAlbumByCountry(int countryID)
        {
            if (countryID <= 0 )
            {
                throw new FaultException("Parameter is zero or negative");
            }
            else
            {
                var q = from a in context.Albums
                        where a.Country.Equals(countryID)
                        select a;
                return q.ToList();
            }
        }
        #endregion


        public List<DataContract_Album> getTopAlbum(int number)
        {
            try {
                if (number <= 0)
                {
                    throw new FaultException("Parameter is zero or nagative ");
                }
                else
                {
                    var q = (from a in context.Albums
                             orderby a.CountView descending
                             select new DataContract_Album { 
                             AlbumID = a.AlbumID,
                             AlbumName = a.AlbumName,
                             CountryID = a.CountryID,
                             CountryName = a.Country.CountryName,
                             CountView = Convert.ToInt32(a.CountView),
                             DateUp = Convert.ToDateTime(a.DateUp),
                             Description = a.Description,
                             Picture = a.Picture,
                             ReleaseYear = Convert.ToInt32(a.ReleaseYear),
                             SingerName = a.Singer.SingerName,
                             SingerID = Convert.ToInt32(a.SingerID)                             
                             }).Take(number);
                    return q.ToList();
                }
            }
            catch (Exception ex) {
                throw new FaultException(ex.Message);
            }
        }

        public List<Album> getnewAlbum(int number)
        {
            try
            {     
                    var q = (from a in context.Albums
                             where a.DateUp.Date.Month.Equals(DateTime.Today.Date.Month -1) && a.DateUp.Date.Year.Equals(DateTime.Today.Date.Year)
                             orderby a.CountView descending
                             select a).Take(number);
                    return q.ToList();               
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #region IAlbumService Members


        public List<Album> getAlbumBySingerName(string singerName)
        {
            try
            {
                if (singerName == "")
                {
                    throw new FaultException("Parameter is null");
                }
                else
                {
                    var q = from a in context.Albums
                            where a.Singer.SingerName.Contains(singerName)
                            select a;
                    return q.ToList();
                            
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion
    }
}
